
package guoan.web.weixin;

import guoan.core.configuration.WeiXinCfgBean;
import guoan.service.weixin.OauthService;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName: OauthContrller
 * @Description: 通过认证界面进行验证跳转
 * @author Administrator
 * @date 2015年4月21日 下午4:36:50
 * 
 */
@Controller
@RequestMapping("/weixin")
public class OauthContrller {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private OauthService oauthService;
    @Autowired
    private WeiXinCfgBean weiXinCfgBean;

    @RequestMapping(value = "oauthStudent")
    public String oauthStudent(){
        return "/weixin/oauthStudent";
    }

    @RequestMapping(value = "oauthTrain")
    public String oauthTrain(){
        return "/weixin/oauthTrain";
    }

    /**
     * @Title：如果没有认证，跳转需要认证的提示界面
     * @Description: TODO
     * @author Administrator
     * @date 2015年4月21日 下午5:28:51
     * @return
     */
    @RequestMapping(value = "notOauth")
    public String notOauth(){
        return "/weixin/notOauth";
    }

    /**
     * @Title：oauth
     * @Description: 用户认证界面
     * @author Administrator
     * @date 2015年4月21日 下午5:31:10
     * @return
     */
    @RequestMapping(value = "oauth")
    public String oauth(String code, String state, HttpServletRequest request){
        log.debug("oauth2 begin code -> {}", code);
        log.debug("oauth2 begin state -> {}", state);
        // 参数错误
        if (code == null || state == null) {
            return "/weixin/notOauth";
        }
        return oauthService.oauth(code, state, request);
    }

    /** 设置页面转发 */
    @RequestMapping(value = "toExam")
    public String toExam(){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + weiXinCfgBean.getAppid() + "&redirect_uri=http%3a%2f%2f"
                + weiXinCfgBean.getHost() + "%2fweixin%2foauth&response_type=code&scope=snsapi_base&state=" + WeiXinCfgBean.LINK_EXAM
                + "&connect_redirect=1#wechat_redirect";
    }

    /** 投票首页转发 */
    @RequestMapping(value = "toCoachTrain")
    public String toCoachTrain(){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + weiXinCfgBean.getAppid() + "&redirect_uri=http%3a%2f%2f"
                + weiXinCfgBean.getHost() + "%2fweixin%2foauth&response_type=code&scope=snsapi_base&state=" + WeiXinCfgBean.LINK_COACH_TRAIN
                + "&connect_redirect=1#wechat_redirect";
    }

    /** 投票首页转发 */
    @RequestMapping(value = "toCoachSudent")
    public String toCoachSudent(){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + weiXinCfgBean.getAppid() + "&redirect_uri=http%3a%2f%2f"
                + weiXinCfgBean.getHost() + "%2fweixin%2foauth&response_type=code&scope=snsapi_base&state=" + WeiXinCfgBean.LINK_COACH_STUDENT
                + "&connect_redirect=1#wechat_redirect";
    }
}
